<?php

class View {
	
	const AJAX_RESPONSE		= 'ajaxResponse';
	const DEFAULT_RESPONSE	= 'show';
	const ERROR_RESPONSE	= 'showError';
	
	
	static public function show($mode, $response, $delorean, $isMobile) {
		
		switch ($mode) {
			case self::ERROR_RESPONSE	: self::showError($response); break;
			case self::AJAX_RESPONSE	: self::ajax($response, $delorean); break;
			
			default						: self::showTpl($response, $delorean, $isMobile);
		}	
	}

	/**
	 * 
	 * @param Response $response
	 * @param TimeMachine $delorean
	 */
	static public function showTpl($response, $delorean, $isMobile) {
		
		$smarty = new Smarty;
		list($burl, $rurl) = self::backRefreshLinks($delorean);
		
		//$smarty->assign('debug', print_r($_SESSION, true));
		$smarty->assign('pageTitle', 'Under Develop');
		$smarty->assign('siteTitle', SITE_TITLE);
		$smarty->assign('showFooter', true);
		$smarty->assign('showNav', true);
		$smarty->assign('showAside', true);
		$smarty->assign('showError', false);
// 		$smarty->assign('backCtrl', $ctrl);
// 		$smarty->assign('backAct', $act);
// 		$smarty->assign('refreshCtrl', $curr_ctrl);
// 		$smarty->assign('refreshAct', $curr_act);
		
		foreach($response->menus as $name => $links) {
			self::replaceBRLinks($links, $burl, $rurl);
			$smarty->assign($name, $links);
		}
		
		$smarty->assign('backUrl', $burl);
		$smarty->assign('refreshUrl', $rurl);
		
		$smarty->assign('viewPath', self::ViewPath($isMobile));
		$smarty->assign('themePath', self::ThemePath($isMobile));
		$smarty->assign('themeUrl', self::ThemeUrl($isMobile));
		$smarty->assign('viewUrl', VIEW_URL);
		//$smarty->assign('weaAppURL', );
		
		$smarty->assign('bodyTpl', self::ViewPath($isMobile).$response->getTpl());
		$smarty->assign($response->getParams());
		
		$tpl_path = self::ThemePath($isMobile).'page.html';
		@$smarty->display($tpl_path);
	}
	
	static public function showError($response) {
	
		$smarty = new Smarty;
		$smarty->assign('pageTitle', 'VFW Error');
		$smarty->assign('siteTitle', SITE_TITLE);		
		$smarty->assign('showFooter', false);
		$smarty->assign('showNav', false);
		$smarty->assign('showAside', false);
		$smarty->assign('showError', false);
	
		$smarty->assign('viewPath', VIEW_PATH);
		$smarty->assign('themePath', THEME_PATH);
		$smarty->assign('themeUrl', THEME_URL);
		$smarty->assign('viewUrl', VIEW_URL);
	
		$smarty->assign('bodyTpl', $response->getTpl());
		$smarty->assign($response->getParams());
	
		$tpl_path = THEME_PATH.'page.html';
		$smarty->display($tpl_path);
	}
	
	/**
	 * 
	 * @param TimeMachine $delorean
	 */
	static public function backRefreshLinks($delorean) {
		
		list($bc, $ba) = $delorean->getPrevAct();
		list($rc, $ra) = $delorean->getCurrentAct();		
		
		$burl = "index.php?ctrl=$bc&act=$ba&back=1";
		$rurl = "index.php?ctrl=$rc&act=$ra";
		
		return array($burl, $rurl);
		
	}
	
	
	
	/**
	 * 
	 * @param Response $response
	 */
	static public function ajax($response, $delorean) {
		$data = $response->getParams();
		$delorean->setRecord(false);
		echo json_encode($data);
	}
	
	static public function replaceBRLinks(&$menu, $burl, $rurl) {
		
		foreach ($menu as $name => &$link) {
			self::replaceLinks($link, $burl, $rurl);
		}
		
	}
	
	private static function replaceLinks(&$v, $burl, $rurl) {
		if ($v == 'LINKBACK') {
			$v = $burl;
		} else if ($v == 'LINKREFRESH') {
			$v = $rurl;
		}
	}
	
	public static function ViewPath($isMobile) {
		if ($isMobile) {
			return VIEW_PATH.'mobile/';
		} else {
			return VIEW_PATH;
		}
	}
	
	public static function ThemePath($isMobile) {
		if ($isMobile) {
			return MOBILE_THEME_PATH;
		} else {
			return THEME_PATH;
		}		
	}
	
	public static function ThemeUrl($isMobile) {
		if ($isMobile) {
			return MOBILE_THEME_URL;
		} else {
			return THEME_URL;
		}		
	}
	
}



?>
